from django.db import models
from .Base_Model import BaModel
from ..model_ddd.models import IdCard


# Create your models here.
# 定义枚举我们采用的是STR的
class EmpChoices(models.TextChoices):
    MA = 'MA', '部门经理'
    PM = 'PM', '普通员工'
    BS = 'BS', '董事'


class SexChoices(models.TextChoices):
    L = 'L', '男'
    O = 'O', '女'


class Employee(BaModel):
    name = models.CharField(max_length=30, unique=True, verbose_name="名字")
    emp = models.CharField(max_length=3, choices=EmpChoices.choices, default=EmpChoices.PM, verbose_name="部门职位")
    last = models.IntegerField(max_length=10, blank=True, null=True, default=0, verbose_name="薪资")
    j_last = models.DecimalField(max_digits=7, decimal_places=2, default=0, null=True, blank=True, verbose_name="津贴")
    b_lz = models.BooleanField(default=False, verbose_name="是否离职")
    sex = models.CharField(max_length=3, choices=SexChoices.choices, default=SexChoices.O, verbose_name="性别")
    dept = models.ForeignKey('DeptModel', null=True, blank=True, related_name='dept_list', on_delete=models.SET_NULL,
                             verbose_name="关联部门表")
    # 一对一的关联关系身份证号码
    id_cord = models.OneToOneField('model_ddd.IdCard', on_delete=models.CASCADE, null=True, blank=True,
                                   verbose_name='关联身份证')
    # 多对多的关联关系 会从这两个表中生成第三张表 在多对多的关联关系中我们就不需要使用on_delete 了
    rules = models.ManyToManyField('model_ddd.RoleModel', db_table='t_role_emp', related_name='role_list', null=True,
                                   blank=True, verbose_name="对多对的关联关系")

    class Meta:
        db_table = 't_emp'
        verbose_name = "部门表"
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return f"姓名{self.name},职位{self.get_emp_display()},新自:{self.last},晶铁{self.j_last},是否离职{self.b_lz},性别:{self.get_sex_display()}"


# 创建部门 两个字段 name adders
class DeptModel(BaModel):
    name = models.CharField(max_length=20, unique=True, verbose_name="部门名字")
    adders = models.CharField(max_length=200, null=True, blank=True, verbose_name="地址")
    prent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL)

    class Meta:
        db_table = 't_dept'
        verbose_name = "部门表"
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return f"当前部门是{self.name},部门所在位置是:{self.adders}"
